package com.ghh.swing.test;

import java.awt.Dimension;

import java.awt.GridLayout;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.concurrent.Executors;

import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MyTestFrame extends JFrame {
    private JButton jButton1 = new JButton();
    private JPanel glassPane = new MyGlassPane();
    private JDialog prograss;

    public MyTestFrame() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(595, 382));
        jButton1.setText("test");
        jButton1.setBounds(new Rectangle(250, 230, 75, 21));
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButton1_actionPerformed(e);
            }
        });
        this.getContentPane().add(jButton1, null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        prograss = createModalDialog();
    }

    private void jButton1_actionPerformed(ActionEvent e) {
        //        System.out.println("start test");
        ////        glassPane.setVisible(!glassPane.isVisible());
        //        JLayeredPane layeredPane = getLayeredPane();
        //        layeredPane.setLayout(new GridLayout(1, 1));
        //        if (b) {
        //            layeredPane.add(glassPane, JLayeredPane.DEFAULT_LAYER);
        //        } else {
        //            layeredPane.remove(glassPane);
        //        }
        //        b = !b;
        //        this.repaint();
        Executors.newScheduledThreadPool(1).schedule(new Runnable() {
                @Override
                public void run() {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            prograss.setVisible(false);
                        }
                    });
                }
            }, 5, TimeUnit.SECONDS);
        prograss.setLocationRelativeTo(this);
        prograss.setVisible(!prograss.isVisible());
    }

    private JDialog createModalDialog() {
        JDialog dialog = new JDialog(this, "Child", true);
        dialog.setLocationRelativeTo(this);
        dialog.add(glassPane);
        dialog.setSize(100, 100);
        dialog.setUndecorated(true);
        return dialog;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyTestFrame frame = new MyTestFrame();
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
            }
        });
    }
}
